package com.gitee.utils;

import com.gitee.constant.HttpStatus;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @ClassName：Result
 * @Author: bin
 * @Date: 2024/11/2 16:08
 * @Description: 响应信息主题
 */
@Data
@NoArgsConstructor
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    public int code;

    public String msg;

    public T data;

    public static <T> Result<T> success() {
        return restResult(null, HttpStatus.SUCCESS, "操作成功");
    }

    public static <T> Result<T> success(T data) {
        return restResult(data, HttpStatus.SUCCESS, "操作成功");
    }

    public static <T> Result<T> success(T data, String msg) {
        return restResult(data, HttpStatus.SUCCESS, msg);
    }

    public static <T> Result<T> success(String msg) {
        return restResult(null, HttpStatus.SUCCESS, msg);
    }

    public static <T> Result<T> failed() {
        return restResult(null, HttpStatus.ERROR, "操作失败");
    }

    public static <T> Result<T> failed(T data) {
        return restResult(data, HttpStatus.ERROR, "操作失败");
    }

    public static <T> Result<T> failed(T data, String msg) {
        return restResult(data, HttpStatus.ERROR, msg);
    }

    public static <T> Result<T> failed(String msg) {
        return restResult(null, HttpStatus.ERROR, msg);
    }

    public static <T> Result<T> restResult(T data, int code, String msg) {
        Result<T> r = new Result<>();
        r.setCode(code);
        r.setData(data);
        r.setMsg(msg);
        return r;
    }
}
